﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Practices.EnterpriseLibrary.Data;
using Microsoft.Practices.EnterpriseLibrary.Common.Configuration;
using Microsoft.Practices.EnterpriseLibrary.Data.Configuration;
using System.Data.Common;
using System.Data;
using System.Collections;

namespace AMStudio.Common
{
    internal class TranslationTagDAL
    {
        //private DataSet myDataset;

        public TranslationTagDAL()
        {

        }
        public bool Add(TranslationItem item)
        {
            Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>("Translation");
            StringBuilder sb = new StringBuilder();
            sb.Append("insert into [TRANSLATION$] values(@TranslationType,@Source,@Destination)");
            DbCommand cmd = db.GetSqlStringCommand(sb.ToString());

            db.AddInParameter(cmd, "@TranslationType", DbType.String, item.TranslationType);
            db.AddInParameter(cmd, "@Source", DbType.String, item.Source);
            db.AddInParameter(cmd, "@Destination", DbType.String, item.Destination);
            
            return db.ExecuteNonQuery(cmd) > 0 ? true : false;
        }

        public bool Update(TranslationItem item)
        {
            Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>("Translation");
            StringBuilder sb = new StringBuilder();
            sb.Append("update [TRANSLATION$] set ");
            sb.Append("[Destination]=@Destination,");
            sb.Append(" where ([TranslationType]=@TranslationType AND [Source]=@Source)");
            DbCommand cmd = db.GetSqlStringCommand(sb.ToString());
            db.AddInParameter(cmd, "@TranslationType", DbType.String, item.TranslationType);
            db.AddInParameter(cmd, "@Source", DbType.String, item.Source);
            db.AddInParameter(cmd, "@Destination", DbType.String, item.Destination);
            
            int r = db.ExecuteNonQuery(cmd);
            return r> 0 ? true : false;;
        }

        public bool Delete(string source)
        {
            Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>("Translation");
            StringBuilder sb = new StringBuilder();
            sb.Append("delete from [TRANSLATION$] ");
            sb.Append(" where ([Source]=@Source)");
            DbCommand cmd = db.GetSqlStringCommand(sb.ToString());
            db.AddInParameter(cmd, "@Source", DbType.String, source);
            return db.ExecuteNonQuery(cmd) > 0 ? true : false;
        }

        /// <summary>
        /// 查询所有信息
        /// </summary>
        /// <returns></returns>
        public Dictionary<string, TranslationItem> GetAllTranslationItems()
        {
            Dictionary<string, TranslationItem> list;
            Database db = EnterpriseLibraryContainer.Current.GetInstance<Database>("Translation");
            DataAccessor<TranslationItem> myAccessor;
            myAccessor = db.CreateSqlStringAccessor("select * from [TRANSLATION$]",
                MapBuilder<TranslationItem>.MapAllProperties().
                Build()
                );
            list = myAccessor.Execute().ToDictionary<TranslationItem, string>((item) =>
            {
                return item.Source;
            });
            return list;
        }
    }
}
